<?php
	/**
	 * Обработка запроса заполнение полей формы.
	 * 
	 * @author Костин Алексей Васильевич aka Volt(220)
	 * @copyright Copyright (c) 2010, Костин Алексей Васильевич
	 * @license http://www.gnu.org/licenses/gpl-3.0.html GNU Public License
	 * @version 1.0
	 * @package HomeMoney
	 */

	if($_SERVER['HTTP_X_REQUESTED_WITH'] != 'XMLHttpRequest') die();
	if(!$_GET['good']) die();
		
	/**
	 * Подключение настроек. 
	 */
	require_once("../ini.php");
	$DBCon=SQLDBFactory::getDB();
	$newItem=$DBCon->escapeString($_GET['good']);
	
	$DBCon->select(getSql($_GET['chto'], $newItem));
	
	if($mas=$DBCon->fetchAssoc()){
		echo json_encode($mas);
	}

	function getSql($chto, $newItem){
		switch ($chto){
			case "cat": 
				$sql="select cat from goods LEFT OUTER JOIN categ ON goods.id_cat = categ.id where item=".$newItem;
			break;

			case "parCat": 
				$sql="select par.cat, c.id as idCat, par.id as idPar 
					from categ as c 
					left outer join categ_tree as ct on ct.id_cat = c.id
					left outer join categ as par on ct.id_par = par.id
					where c.cat=".$newItem." and
						ct.level=1";
			break;
			
			case "idParCat": 
				$sql="select id as idPar from categ where cat=".$newItem;
			break;
			
			case "idCat": 
				$sql="select id as idEx from categ where cat=".$newItem;
			break;
			
			case "idGood": 
				$sql="select id as idEx from goods where item=".$newItem;
			break;

			case "idExC": 
				$sql="select se.id as idEx from spisExceptions as se
					left outer join categ on categ.id=se.id_smth and se.typeEx='c' where categ=".$newItem;
			break;
			
			case "idExG": 
				$sql="select se.id as idEx from spisExceptions as se
					left outer join goods on goods.id=se.id_smth and se.typeEx='g' where item=".$newItem;
			break;
			
			
			case "addr": 
				$sql="select addr from address 
					left outer join shops_address on address.id=shops_address.id_addr
					left outer join shops on shops.id=shops_address.id_shop
					where name=".$newItem;
			break;
			
			default: 
				$sql="select cat, name, addr, col, price
					FROM basket
					LEFT OUTER JOIN goods ON goods.id = basket.id_good
					LEFT OUTER JOIN buys ON buys.id = basket.id_buy
					LEFT OUTER JOIN shops ON shops.id = buys.id_shop
					LEFT OUTER JOIN shops_address on shops_address.id_shop=shops.id
					LEFT OUTER JOIN address on address.id=shops_address.id_addr
					LEFT OUTER JOIN categ ON goods.id_cat = categ.id
					where item=".$newItem." 
					order by buys.date desc
					limit 1";
			break;
		}
		return $sql;
	}

?>
